package com.phpxiu.app.view;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;

import com.phpxiu.app.config.HttpConfig;
import com.phpxiu.app.kkylive.R;
import com.phpxiu.app.okhttp.OKHttp;
import com.phpxiu.app.okhttp.OKHttpResponseModel;
import com.phpxiu.app.okhttp.handler.OKHttpUIHandler;
import com.phpxiu.app.okhttp.param.OKHttpParam;
import com.phpxiu.app.view.custom.WindowLayout;


/**
 * Created by WuJinZhou on 2016/6/4.
 */
public class UserInfoJobEdit extends UIBase implements View.OnClickListener,TextWatcher {
    public final static String TAG="UserInfoJobEdit";
    public final static String EXTRA_PARAM_USER_JOB="extra_param_user_job";
    public final static String RESULT_PARAM_USER_JOB="result_param_user_job";
    private WindowLayout mainView;
    private String job;
    private EditText mInput;
    private Button ok;
    private ImageButton clear;
    private int maxLength=16;
    private TextView countView;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.user_info_job_edit);
        mainView=(WindowLayout)findViewById(R.id.user_info_job_edit);
        job=getIntent().getStringExtra(EXTRA_PARAM_USER_JOB);
        init();
    }

    /**
     * 初始化
     */
    private void init(){
        findViewById(R.id.top_bar_back_btn).setOnClickListener(this);
        ok=(Button)findViewById(R.id.top_bar_submit_btn);
        ok.setOnClickListener(this);
        clear=(ImageButton)findViewById(R.id.clear_job_input_btn);
        clear.setOnClickListener(this);
        if(job!=null&&!"".equals(job)){
            clear.setVisibility(View.VISIBLE);
        }else{
            clear.setVisibility(View.INVISIBLE);
        }
        mInput=(EditText)findViewById(R.id.job_input);
        mInput.setText(job);
        mInput.setSelection(Math.min(16,job.length()));
        mInput.addTextChangedListener(this);
        countView=(TextView)findViewById(R.id.input_msg_view);
        if(job!=null){
            int c=maxLength-job.length();
            countView.setText(""+c);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.top_bar_submit_btn:
                submit();
                break;
            case R.id.clear_job_input_btn:
                mInput.setText(null);
                clear.setVisibility(View.INVISIBLE);
                break;
            case R.id.top_bar_back_btn:
                setResult(RESULT_CANCELED,null);
                onBackPressed();
                break;
            default:
                break;
        }
    }

    /**
     * 提交保存
     */
    public void submit(){
        if(TextUtils.isEmpty(mInput.getText())){
            return;
        }
        OKHttpParam param=OKHttpParam.builder(false);
        param.remove("platform");
        param.put("job",mInput.getText().toString());
        ok.setClickable(false);
        OKHttp.post(HttpConfig.API_UPDATE_USER_FIELD, param.jsonParam(), TAG, new OKHttpUIHandler(OKHttpResponseModel.class) {
            @Override
            public void onSuccess(Object model) {
                ok.setClickable(true);
                Intent intent = new Intent();
                intent.putExtra(RESULT_PARAM_USER_JOB, mInput.getText().toString());
                setResult(RESULT_OK, intent);
                finish();
            }

            @Override
            public void onErr(String errMsg) {
                ok.setClickable(true);
                alert(errMsg,null);
            }

        });
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        int length=TextUtils.getTrimmedLength(s);
        int c=(maxLength-length);
        if(c>-1){
            countView.setText(""+c);
        }
        if(length>0){
            if(clear.getVisibility()!=View.VISIBLE) {
                clear.setVisibility(View.VISIBLE);
            }
        }else{
            if(clear.getVisibility()==View.VISIBLE) {
                clear.setVisibility(View.INVISIBLE);
            }
        }
    }

    @Override
    public void afterTextChanged(Editable s) {

    }
}
